/***************************************************************************
 *   Copyright (C) 2009 by Marian Skvarek   *
 *   skvarek.marian@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A TrackICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "CMusicXmlImporter.h"
#include "CScore.h"
#include "CPart.h"
#include "CTrack.h"
#include "CBar.h"
#include "CNote.h"
#include "CPause.h"
#include "CUtils.h"

#include <QtXml/QDomDocument>
#include <QFile>
#include <QTextStream>
#include <QLinkedList>

using namespace easyscore::core;

namespace easyscore
{

	namespace components
	{

		namespace io
		{

			CMusicXmlImporter::CMusicXmlImporter()
			{
			}


			CMusicXmlImporter::~CMusicXmlImporter()
			{
			}

//Public methods
                        bool CMusicXmlImporter::importScore(QFile &file, CScore *score)
                        {
                            Q_UNUSED(file);
                            Q_UNUSED(score);

//                            QDomDocument doc;
//                            QString errorStr;
//                            int errorLine;
//                            int errorColumn;

//                            doc.setContent(&file, true, &errorStr, &errorLine, &errorColumn);

//                            QDomNodeList scores = doc.elementsByTagName("score-partwise");
//                            if(scores.count() < 1)
//                                return 0;

//                            QDomElement scoreEl = scores.at(0).toElement();


//                            QDomNodeList partLists = scoreEl.elementsByTagName("part-list");
//                            int partCount = partLists.count();


//                            int i = 1;
//                            foreach(CPart *part, score->getParts())
//                            {
//                                QDomElement partDecEl = doc.createElement("score-part");
//                                partListEl.appendChild(partDecEl);
//                                partDecEl.setAttribute("id",i);
//
//                                QDomElement partNameEl = doc.createElement("part-name");
//                                partDecEl.appendChild(partNameEl);
//
//                                QDomText partNameText = doc.createTextNode(part->getName());
//                                partNameEl.appendChild(partNameText);
//
//                                QDomElement partEl = doc.createElement("part");
//                                scoreEl.appendChild(partEl);
//                                partEl.setAttribute("id",i);
//
//                                int barCount = part->getTracks().first()->getBars().count();
//                                QLinkedList<QLinkedList<CBar *>::iterator> bars;
//                                foreach(CTrack *track, part->getTracks())
//                                {
//                                    bars.append(track->getBars().begin());
//                                }
//
//                                for(int j = 0; j < barCount; ++j)
//                                {
//                                    QDomElement barEl = doc.createElement("measure");
//                                    partEl.appendChild(barEl);
//                                    barEl.setAttribute("number",j + 1);
//
//                                    foreach(QLinkedList<CBar *>::iterator it, bars)
//                                    {
//                                        foreach(CSymbol *symbol, (*it)->getSymbols())
//                                        {
//
//                                            if(symbol->isRest())
//                                            {
//                                                QDomElement symbolEl = doc.createElement("note");
//                                                barEl.appendChild(symbolEl);
//
//                                                QDomElement noteTypeEl = doc.createElement("type");
//                                                symbolEl.appendChild(noteTypeEl);
//
//                                                QDomText noteTypeText = doc.createTextNode(getSymbolValueText(symbol->getValue()));
//                                                noteTypeEl.appendChild(noteTypeText);
//
//                                                QDomElement restEl = doc.createElement("rest");
//                                                symbolEl.appendChild(restEl);
//                                            }
//                                            else
//                                            {
//                                                CNoteGroup *noteGroup = (CNoteGroup *) symbol;
//
//                                                bool firstNote = true;
//                                                foreach(CNote *note, noteGroup->getNotes())
//                                                {
//
//                                                QDomElement symbolEl = doc.createElement("note");
//                                                barEl.appendChild(symbolEl);
//
//                                                QDomElement noteTypeEl = doc.createElement("type");
//                                                symbolEl.appendChild(noteTypeEl);
//
//                                                QDomText noteTypeText = doc.createTextNode(getSymbolValueText(symbol->getValue()));
//                                                noteTypeEl.appendChild(noteTypeText);
//
//                                                QDomElement pitchEl = doc.createElement("pitch");
//                                                symbolEl.appendChild(pitchEl);
//
//                                                QDomElement stepEl = doc.createElement("step");
//                                                pitchEl.appendChild(stepEl);
//
//                                                QDomText stepElText = doc.createTextNode(getNoteStepText(note->getRelativePitch()));
//                                                stepEl.appendChild(stepElText);
//
//                                                QDomElement octaveEl = doc.createElement("octave");
//                                                pitchEl.appendChild(octaveEl);
//
//                                                QDomText octaveElText = doc.createTextNode(QString::number(note->getOctave()));
//                                                octaveEl.appendChild(octaveElText);
//
//                                                if(!firstNote)
//                                                {
//                                                    QDomElement chordEl = doc.createElement("chord");
//                                                    symbolEl.appendChild(chordEl);
//                                                }
//
//                                                firstNote = false;
//
//
//                                                }
//                                            }
//
//                                        }
//
//                                                ++it;
//                                    }
//                                }
//
//
//                                ++i;
//                            }
//
//                            //write to file
//                            QTextStream out(&file);
//                            out << doc.toString();

                            return true;
                        }
		}

	}

}
